package com.example.goandpaytthebill.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.viewpager.widget.PagerAdapter;

import com.example.goandpaytthebill.Bean.BannerListBean;
import com.example.goandpaytthebill.R;

import com.example.goandpaytthebill.Tool.CustomRoundAngleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

public class LauncherViewPagerSYNewAdapter extends PagerAdapter {
    private Context mContext;
    private View itemView;
    private List<BannerListBean.DataBean> pagesArrayList;
    public LauncherViewPagerSYNewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 设置ViewPager将要显示的数据.
     * 当图片数量小于三张的时候,通过复制组拼数据
     * @param arrayList
     */
    public void setAdapterData(List<BannerListBean.DataBean> arrayList){
        pagesArrayList=arrayList;
        if (pagesArrayList.size()<1) {
            Toast.makeText(mContext, "ViewPager item size=0", Toast.LENGTH_LONG).show();
        }else if(pagesArrayList.size()<2){
            pagesArrayList.add(pagesArrayList.get(0));
            pagesArrayList.add(pagesArrayList.get(0));
            pagesArrayList.add(pagesArrayList.get(0));
        }else if(pagesArrayList.size()<3){
            pagesArrayList.add(pagesArrayList.get(0));
            pagesArrayList.add(pagesArrayList.get(1));
        }
        System.out.println("-----> PagerAdapter中item的个数="+pagesArrayList.size());
    }

    @Override
    public int getCount() {
        return pagesArrayList.size();
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (pagesArrayList.size() > 0) {
            itemView= LayoutInflater.from(mContext).inflate(R.layout.guide_pager_adapter, null);
            itemView.setFocusable(true);
            CustomRoundAngleImageView imageView = (CustomRoundAngleImageView) itemView.findViewById(R.id.imageView);
            Picasso.with(mContext)
                    .load(String.valueOf("http://tbyg-new.oss-cn-beijing.aliyuncs.com/"+pagesArrayList.get(position%pagesArrayList.size()).getPic()))
                    .fit()
                    .into(imageView);
//            imageView.setBackgroundResource(Integer.parseInt(pagesArrayList.get(position%pagesArrayList.size()).getImgsrc()));
            container.addView(itemView);
            return itemView;
        }
        return null;

    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
